package cn.bs.busuo.front.account.service.impl;

import cn.bs.busuo.front.account.dao.persist.repository.ITagRepository;
import cn.bs.busuo.front.account.dao.persist.repository.IUserRepository;
import cn.bs.busuo.front.account.pojo.vo.AllTagVO;
import cn.bs.busuo.front.account.pojo.vo.UserCharacterVO;
import cn.bs.busuo.front.account.service.ITagService;
import cn.bs.busuo.common.enumerator.ServiceCode;
import cn.bs.busuo.common.ex.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;

@Service
public class TagServiceImpl implements ITagService {
    @Autowired
    private ITagRepository tagRepository;
    @Autowired
    private IUserRepository userRepository;
    @Override
    public int AddNewTag(Long id, List<Long> characterId) {
        boolean b = new HashSet<>(characterId).size() != characterId.size();
        if (b) {
            String message = "添加标签失败，标签ID重复！";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        int TagCount = tagRepository.selectTagIfExits(characterId);
        if (TagCount != characterId.size()) {
            String message = "添加标签失败，有不存在的标签！";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        int exits = tagRepository.selectUserTagIfExits(id, characterId);
        if (exits > 0) {
            String message = "添加标签失败，含有已经添加过的标签！";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        LocalDateTime now = LocalDateTime.now();
        return tagRepository.AddNewTag(id,characterId,now);
    }

    @Override
    public int deleteTag(Long id, List<Long> characterId) {
        boolean b = new HashSet<>(characterId).size() != characterId.size();
        //判断是否有重复的标签
        if (b) {
            String message = "删除标签失败，标签ID重复！";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        //判断是否传入了空标签
        if (characterId.size() == 0) {
            String message = "删除标签失败，标签ID不能为空！";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        int TagCount = tagRepository.selectTagIfExits(characterId);
        if (TagCount != characterId.size()){
            String message = "删除标签失败，有不存在的标签！";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        return tagRepository.deleteTag(id,characterId);
    }

    @Override
    public List<UserCharacterVO> listTag(Long id) {
        //判断传入的参数是否为空
        if (id == null) {
            String message = "获取用户标签失败，用户ID不能为空！";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        //判断用户是否存在
        if (userRepository.ifUserExits(id) == 0) {
            String message = "获取用户标签失败，尝试访问的用户数据不存在！";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return tagRepository.listTag(id);
    }

    @Override
    public List<AllTagVO> listAllTag() {
        return tagRepository.listAllTag();
    }

}
